import jsonwebtoken from 'jsonwebtoken';
const { sign } = jsonwebtoken;
import resultEnum from '../utils/resultEnum.js';
import Mocks from '../utils/mock.js';
const Mock = new Mocks();
import { jwtSecret } from '../config/config.js';

/* 查询用户信息 */
export function selectUserInfo (ctx, next) {
  try {
    const params = {
      ...ctx.query
    };
    console.log(ctx.state.user);
    // console.log(ctx.request.header.Authorization);
    /* 模拟查询数据库，处理后返回 */
    /* 返回对象 */
    // if (ctx.request.header.Authorization === 'Bearer 11') {
    //   ctx.error(resultEnum.GET_ACCESS_TOKEN_Expired.code, resultEnum.GET_ACCESS_TOKEN_Expired.msg)
    // } else {
    // }
    ctx.success(resultEnum.GET_SUCCESS.code, resultEnum.GET_SUCCESS.msg, Mock.getUserList())
  } catch (error) {
    ctx.error(resultEnum.GET_ERROR.code, resultEnum.GET_ERROR.msg)
  }
}

/* 查询用户信息（注册用户） */
export function selectUserListInfo (ctx, next) {
  try {
    const params = {
      ...ctx.query
    };

    // console.log(ctx.state);
    /* 模拟查询数据库，处理后返回 */
    /* 返回对象 */
    //  console.log(ctx.response);
    ctx.success(resultEnum.GET_SUCCESS.code, resultEnum.GET_SUCCESS.msg, Mock.getUserList1())
  } catch (error) {
    ctx.error(resultEnum.GET_ERROR.code, resultEnum.GET_ERROR.msg)
  }
}

/* 查询用户信息 */
export function selectUser (ctx, next) {
  const params = {
    ...ctx.query
  };
  console.log(params);
  ctx.success(resultEnum.GET_SUCCESS.code, resultEnum.GET_SUCCESS.msg, Mock.getUserById())
}
/* 查询用户信息通过id */
export function selectUserInfoById (ctx, next) {
  const params = {
    ...ctx.params
  };
  console.log(params);
  ctx.success(resultEnum.GET_SUCCESS.code, resultEnum.GET_SUCCESS.msg, Mock.getUserById())
}

/* 新增用户信息 */
export function insertUser (ctx, next) {
  const params = {
    ...ctx.request.body
  };
  console.log(params);
  ctx.success(resultEnum.SAVE_SUCCESS.code, resultEnum.SAVE_SUCCESS.msg)
}
/* 获取token信息 */
export function getToken (ctx, next) {
  const params = {
    ...ctx.request.body
  };
  console.log(params);
  /* 查询用户信息，生成token */
  const token = sign({
    name: params.username,
    id: 56789157897878
  }, // 加密userToken
    jwtSecret, {
    expiresIn: '1h'
    // expiresIn: '10s'
  }
  )
  // ctx.success(resultEnum.GET_TOKEN_SUCCESS.code, resultEnum.GET_TOKEN_SUCCESS.msg, Mock.getToken())
  ctx.success(resultEnum.GET_TOKEN_SUCCESS.code, resultEnum.GET_TOKEN_SUCCESS.msg, {
    accessToken: token
  })
}
/* 刷新refreshToken信息 */
export function getRefreshToken (ctx, next) {
  const params = {
    ...ctx.request.body
  };
  console.log(params);
  ctx.success(resultEnum.GET_TOKEN_SUCCESS.code, resultEnum.GET_TOKEN_SUCCESS.msg, Mock.getToken())
}

/* 更新用户信息 */
export function updateUser (ctx, next) {
  const params = {
    ...ctx.request.body,
    ...ctx.params
  };
  console.log(params);
  ctx.success(resultEnum.UPDATE_SUCCESS.code, resultEnum.UPDATE_SUCCESS.msg)
}

/* 删除用户信息 */
export function deleteUser (ctx, next) {
  const params = {
    ...ctx.params
  };
  console.log(params);
  ctx.success(resultEnum.DELETE_SUCCESS.code, resultEnum.DELETE_SUCCESS.msg)
}